import { emit } from "process";

enum State {
  pending = 'pending',
  fulfilled = 'fulfilled',
  rejected='rejected',
}
class MyPromise {
  //最终返回值
  private PromiseResult: any;
  private PromiseState: State;
  constructor() {
    this.PromiseState = State.pending
    this.PromiseResult = null;
    //初始化this
    this.resolve = this.resolve.bind(this);
    this.reject = this.reject.bind(this);
  }

  resolve(value: any) {
    //state 是不可变的 只有pending是未执行的才能进入
    if (this.PromiseState !== 'pending') return
    this.PromiseState = State.fulfilled;
    this.PromiseResult = value;
  }
  reject(reason: any) {

    if (this.PromiseState !== 'pending') return
    this.PromiseState = State.rejected;
    this.PromiseResult = reason;
  }
  
}